/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.model.OrderItem;
import com.startup.musicstore.services.crud.OrderAmountService;
import com.startup.musicstore.services.crud.OrderCrudService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class OrderAmountServiceImpl implements OrderAmountService{
    private static ApplicationContext ctx;
    
    @Override
    public BigDecimal getOrderAmount(Long orderId) {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");
        double total = 0;
        OrderCrudService orderCrudService = (OrderCrudService) ctx.getBean("orderCrudService");
        Orders order = orderCrudService.findById(orderId);
        List<OrderItem> orderItems = order.getOrderItems();
        for(OrderItem oi: orderItems){
            Album album = oi.getAlbum();
            String unitPrice = album.getUnitPrice().toPlainString();
            total += oi.getQuantity() * Double.parseDouble(unitPrice);
        }
        return new BigDecimal(total);
    }    
}
